package com.javabrains.struts.action;

import org.apache.commons.lang3.StringUtils;

import com.javabrains.struts.model.User;
import com.javabrains.struts.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class loginAction extends ActionSupport implements ModelDriven {
	User user = new User();

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	public void validate() {
		if (StringUtils.isEmpty(user.getUserId())) {
			addFieldError("userId", "LoginId cannot be null");
		}
		if (StringUtils.isEmpty(user.getPassword())) {
			addFieldError("password", "password cannot be null");
		}
	}

	public String execute() {
		LoginService loginService = new LoginService();

		if (loginService.verifyLogin(user)) {
			return SUCCESS;
		}
		return LOGIN;
	}

	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		return user;
	}

}
